package cn.adelyn.blog.file.controller;

import cn.adelyn.base.api.snowfalke.feign.SnowflakeFeignClient;
import cn.adelyn.base.secureweb.AuthUserContext;
import cn.adelyn.blog.file.constant.MinioConstant;
import cn.adelyn.blog.file.service.FileService;
import cn.adelyn.common.core.execption.AdelynException;
import cn.adelyn.common.core.response.ServerResponseEntity;
import cn.adelyn.common.crypto.utils.URLAesEncodeUtil;
import cn.hutool.core.io.IoUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.validation.constraints.NotBlank;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Validated
@RestController
@RequestMapping("/pic")
public class PicController {

    @Autowired
    FileService fileService;

    @Autowired
    SnowflakeFeignClient snowflakeFeignClient;
/*
    @GetMapping("/uploadUrl")
    public ServerResponseEntity<FileUrlVO> getUploadUrl() {
        return ServerResponseEntity.success(
                fileService.genUploadUrl(MinioConstant.PIC_PATH_PREFIX + MinioConstant.PATH_SEPARATOR)
        );
    }

    @GetMapping("/downloadUrl/{picId}")
    public ServerResponseEntity<String> getDownloadUrl(@PathVariable(value = "picId")
                                                           @NotNull(message = "picId 不能为空~") Long picId) {
        return ServerResponseEntity.success(fileService.genDownloadUrl(
                MinioConstant.PIC_PATH_PREFIX + MinioConstant.PATH_SEPARATOR + picId)
        );
    }

    @GetMapping("/download/{picId}")
    public void getDownloadUrl(@PathVariable(value = "picId")
                                   @NotNull(message = "picId 不能为空~") Long picId, HttpServletResponse httpServletResponse) {
        String downloadUrl = fileService.genDownloadUrl(
                MinioConstant.PIC_PATH_PREFIX + MinioConstant.PATH_SEPARATOR + picId + ".png");
        try {
            httpServletResponse.setContentType("image/png");
            httpServletResponse.sendRedirect(downloadUrl);
        } catch (IOException e) {
            throw new AdelynException("请求转发失败");
        }
    }*/

    @PostMapping
    public ServerResponseEntity<String> addPic(@RequestParam MultipartFile pic) {
        long picId;

        try {
            picId = fileService.putFile(pic.getInputStream(), pic.getOriginalFilename(),
                    MinioConstant.PIC_PATH_PREFIX + MinioConstant.PATH_SEPARATOR,
                    pic.getContentType(),
                    AuthUserContext.get().getUserId()
            );
        } catch (Exception e) {
            throw new AdelynException("上传图片失败: " + e.getMessage());
        }

        String picIdUrl = URLAesEncodeUtil.encryptEncode(String.valueOf(picId));
        return ServerResponseEntity.success(picIdUrl);
    }

    @GetMapping("/{picId}")
    public void getPic(@PathVariable(value = "picId")
                           @NotBlank(message = "picId 不能为空~") String urlPicId, HttpServletResponse httpServletResponse) {
        String picId = URLAesEncodeUtil.encryptDecode(urlPicId);

        InputStream inputStream = fileService.getFile(
                MinioConstant.PIC_PATH_PREFIX + MinioConstant.PATH_SEPARATOR + picId
            );

        httpServletResponse.setContentType("image/png");

        try(OutputStream outputStream = httpServletResponse.getOutputStream()){
            IoUtil.copy(inputStream, outputStream);
        } catch (IOException e) {
            throw new AdelynException("下载图片失败: " + e.getMessage());
        }
    }
}
